package com.wagn.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wagn.eduservice.entity.EduChapter;
import com.wagn.eduservice.entity.EduVideo;
import com.wagn.eduservice.entity.chapter.ChapterClassifyEntity;
import com.wagn.eduservice.entity.chapter.VoStep2AddChapter;
import com.wagn.eduservice.entity.chapter.VoStep2AddVideo;
import com.wagn.eduservice.mapper.EduChapterMapper;
import com.wagn.eduservice.service.EduChapterService;
import com.wagn.eduservice.service.EduCourseService;
import com.wagn.eduservice.service.EduVideoService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-08-17
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduCourseService eduCourseService;

    @Autowired
    private EduVideoService eduVideoService;

    //@Autowired
    //private VodService vodService;

    //获取chapter和video
    @Override
    public ChapterClassifyEntity getChapterAndVideoById(String courseId) {

        ChapterClassifyEntity rs = new ChapterClassifyEntity();
        rs.setId(courseId);
        rs.setLabel(eduCourseService.getById(courseId).getTitle());

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("course_id",courseId);
        wrapper.orderByAsc("sort");
        //从章节表获取所有章节table_list1
        List<EduChapter> table_list1 = this.list(wrapper);
        if(table_list1==null){
            return null;
        }

        //table_list1转换为vo_list1
        List<ChapterClassifyEntity> vo_list1 = new ArrayList<>();

        for(EduChapter e:table_list1){
            ChapterClassifyEntity temp1 = new ChapterClassifyEntity();
            temp1.setId(e.getId());
            temp1.setLabel(e.getTitle());
            vo_list1.add(temp1);
        }
        rs.setChildren(vo_list1);

//-------------------------------------------------------------------

        wrapper = new QueryWrapper();
        wrapper.eq("course_id",courseId);
        wrapper.orderByAsc("sort");
        //从小节表获取所有的小节table_list2
        List<EduVideo> table_list2 = eduVideoService.list(wrapper);
        //table_list2转换为vo_list2
        //List<ChapterClassifyEntity> vo_list2 = new ArrayList<>();

        for(ChapterClassifyEntity c:vo_list1){
            //每章一个小节vo_list2
            List<ChapterClassifyEntity> vo_list2 = new ArrayList<>();
            for(EduVideo e :table_list2){
                if( e.getChapterId().equals(c.getId()) ){
                    ChapterClassifyEntity temp2 = new ChapterClassifyEntity();
                    temp2.setId(e.getId());
                    temp2.setLabel(e.getTitle());
                    temp2.setIsFree(e.getIsFree());
                    temp2.setVideoSourceId(e.getVideoSourceId());
                    temp2.setVideoOriginalName(e.getVideoOriginalName());

                    vo_list2.add(temp2);
                }
            }
            //把小节vo_list2放入对应的章中
            c.setChildren(vo_list2);
        }


        rs.setChildren(vo_list1);
        return rs;
    }

    //删除chapter
    @Override
    public Boolean deleteChapter(String c, String id) {
        Boolean rs = false;
        if(c.equals("chapter")){
            //删除章节
            rs = this.removeById(id);
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("chapter_id",id);
            //删除小节中的云视频
            EduVideo one = eduVideoService.getOne(wrapper);
            //vodService.remove(one.getVideoSourceId());
            //删除小节
            eduVideoService.remove(wrapper);
        }else if(c.equals("video")){
            rs = eduVideoService.removeById(id);
        }
        return rs;
    }

    //step2的添加chapter
    @Override
    public boolean step2AddChapter(VoStep2AddChapter voStep2AddChapter) {
        Boolean rs = false;

        EduChapter eduChapter = new EduChapter();              //将数据封装到eduChapter
        eduChapter.setCourseId(voStep2AddChapter.getCourseId());
        eduChapter.setTitle(voStep2AddChapter.getTitle());


        if(!("-1").equals(voStep2AddChapter.getThisChapterId())) {      //如果不是-1
            EduChapter thisChapter = this.getById(voStep2AddChapter.getThisChapterId());

            int sort=thisChapter.getSort();

            if (voStep2AddChapter.getSign().equals("down")) {    //下方添加
                sort=sort+1;
            }
            //后移chapters
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("course_id",voStep2AddChapter.getCourseId());
            wrapper.ge("sort",sort);
            List<EduChapter> list = this.list(wrapper);
            if(list.size()!=0){
                for(EduChapter e : list){
                    e.setSort(e.getSort()+1);
                }
                this.updateBatchById(list);
            }
            eduChapter.setSort(sort);
            rs = this.save(eduChapter);
        }else{                                                      //如果是-1
            eduChapter.setSort(1);
            rs = this.save(eduChapter);
        }
        return rs;
    }

    //step2的添加video
    @Override
    public boolean step2AddVideo(VoStep2AddVideo voStep2AddVideo) {
        //    private String courseId;
        //    private String thisChapterId; //当前行的章节id
        //    private String thisVideoId;   //当前行的小节id,如果为-1，就从最后添加
        //    private String sign;  //up或down
        //    private String title;
        Boolean rs = false;

        EduVideo eduVideo = new EduVideo();
        eduVideo.setCourseId(voStep2AddVideo.getCourseId());
        eduVideo.setTitle(voStep2AddVideo.getTitle());
        eduVideo.setChapterId(voStep2AddVideo.getThisChapterId());
        eduVideo.setIsFree(voStep2AddVideo.getIsFree());
        eduVideo.setVideoSourceId(voStep2AddVideo.getVideoSourceId());
        eduVideo.setVideoOriginalName(voStep2AddVideo.getVideoOriginalName());

        System.out.println("/n/nwakaka:/n"+eduVideo.toString()+"/n/n");
        if("-1".equals(voStep2AddVideo.getThisVideoId()) ){
            eduVideo.setSort(1);
            eduVideoService.save(eduVideo);
        }else{
            EduVideo thisVideo = eduVideoService.getById(voStep2AddVideo.getThisVideoId());
            int sort = thisVideo.getSort();
            if("down".equals(voStep2AddVideo.getSign())){
                sort++;
            }
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("course_id",voStep2AddVideo.getCourseId());
            wrapper.eq("chapter_id",voStep2AddVideo.getThisChapterId());
            wrapper.ge("sort",sort);
            List<EduVideo> list = eduVideoService.list(wrapper);
            if(list.size()!=0){
                for(EduVideo e : list){
                    e.setSort(e.getSort()+1);
                }
                eduVideoService.updateBatchById(list);
            }
            eduVideo.setSort(sort);

            rs = eduVideoService.save(eduVideo);
        }
        return rs;
    }

}
